package com.dpiinc.TechPage.user.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.room.bean.PenEntity;
import com.dpiinc.TechPage.widget.ClearEditText;
import com.beiji.lib.pen.PenManager;
import com.beiji.lib.pen.model.PenInfo;

import androidx.annotation.Nullable;

/**
 * Created by hu489_000 on 2018-06-25.
 */

public class PenNameModifyActivity extends BaseActivity {

    View user_agreement_layout;
    View logout_layout;
    ClearEditText edit_pen_name;
    PenInfo mPenInfo;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.pen_name_modify);
        super.onCreate(savedInstanceState);

        setBackWhite();
        setstatusBarColor(R.color.color_00bad2);


        mPenInfo = PenManager.Companion.getInstance().getConnectedPen();

        edit_pen_name = findViewById(R.id.edit_pen_name);
        edit_pen_name.setText(mPenInfo.getName());
        edit_pen_name.setPhoneInput(false);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        String sPenName = edit_pen_name.getText().toString();
        if(TextUtils.isEmpty(sPenName)){
            return;
        }
        mPenInfo.setName(sPenName);

        RoomAiWriterDatabase db = RoomAiWriterDatabase.getInstance(PenNameModifyActivity.this);
        PenEntity mPenEntity = db.penDao().getPenByMacAddress(mPenInfo.getMacAddress());
        if(mPenEntity == null){
            mPenEntity = new PenEntity();
            mPenEntity.setPenId(mPenInfo.getMacAddress());
            mPenEntity.setMacAddress(mPenInfo.getMacAddress());
            mPenEntity.setName(sPenName);
            db.penDao().insert(mPenEntity);
        }else{
            mPenEntity.setName(sPenName);
            db.penDao().update(mPenEntity);
        }
    }
}
